<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="../css/bootstrap.min.css">
	<script src="../js/jquery-1.11.1.min.js"></script> 
     <script src="../js/bootstrap.min.js"></script>
	<link rel="stylesheet" href="../css/wpl_style.css">
<?php include 'dbInclude.php';?>
</head>
<body  class="gridBody">
<div id="btndiv">
<label style="width:90%;text-align:center">Portfolio valuation</label>
<img id="refresh" src="../img/refresh_new.png"></img>
</div>
<div id="fillref" class="CSSTableGenerator">
<table>
<tr class="tbhd"><td>Company</td><td>Quantity</td><td>Balance</td><td>Current Balance</td><td>Profit/Loss</td></tr>
<?php

ob_start();
    session_start();
	
    if(isset($_SESSION['sess_client_id']) && !empty($_SESSION['sess_client_id'])) {
		$clid = $_SESSION['sess_client_id'];
	$rows=$dbh->query("select im.INSTR_NAME, pm.INSTR_ID, pm.QUANTITY, pm.BALANCE from portfolio_master pm join instrument_master im on pm.INSTR_ID=im.INSTR_ID where CLIENT_ID = ".$clid." ;");
foreach ($rows as $row) {
echo "<tr>";
$instName = $row['INSTR_NAME'];
$instid = $row['INSTR_ID'];
$qnty = $row['QUANTITY'];
$bal = $row['BALANCE'];
echo "<td>".$instName."</td>";
echo "<td>".$qnty."</td>";
echo "<td>".$bal."</td>";

$rows1 = $dbh->query("select * from market where INSTR_ID=".$instid." ;");

foreach ($rows1 as $row) {
	$ltp = $row["LTP"];	
}
$final = $ltp * $qnty;
$ans = $final-$bal;
echo "<td>".$final."</td>";
echo "<td  class='price'>".$ans."</td>";
echo "</tr>";
}
}

?>
</table>
</div>
</body>
<script>
var priceArray = [];
var newPriceArray = [];
var upDownArray = [];
function changeColor(){
	
	var y = document.getElementsByClassName("price");
	for(var i=0;i<y.length;i++){
		if(y[i].innerHTML<0){
			y[i].style.color="red";
		}else if(y[i].innerHTML>0){
			y[i].style.color="green";
		}else{
			y[i].style.color="black";
		}
		//newPriceArray.push(y[i].innerHTML);
	}
	// alert(newPriceArray);
	// for(var i=0;i<y.length;i++){
		// upDownArray.push(priceArray[i]-newPriceArray[i]);
	// }
	// alert(upDownArray);

}



$(document).ready(function(){
$("#refresh").click(function(){
 priceArray = [];
 newPriceArray = [];
 upDownArray = [];
	var x = document.getElementsByClassName("price");

	for(var i=0;i<x.length;i++){
		priceArray.push(x[i].innerHTML);
	}
	//alert(priceArray);
	$.ajax({
	  type: "POST",
	  url: "ajax_portfolio.php",
	  
	}).done(function( data ) {
    $("#fillref").html(data);
	changeColor();
	});
	
});
changeColor();

});
</script>
</html>